<?php
namespace home\model;

use home\model\Model;
use home\model\TypeModel;

class BlogModel extends Model{

    /**
     * 查询10条博客信息并返回
     */
    public function getBlogPage($params = null){
        $curr = $params['page']*10;
        $sql = "SELECT id,title,outline,blog_img FROM blog_info WHERE state = 1 limit {$curr},10";
        $data = $this->queryAll($sql);
        return $data;
    }
     /**
     * 查询单个博客的信息
     */
    public function getBlog($params){
        $sql = "SELECT blog_info.id,user.name,blog_info.title,blog_info.outline,blog_info.content,blog_info.time,blog_info.reader,blog_info.blog_type,blog_info.praise_count,blog_info.state 
        FROM blog_info INNER JOIN user 
        WHERE user.id= blog_info.user_id AND blog_info.state = 1 AND blog_info.id = :id";
        $data = $this->p_query($sql,array('id'=>$params['id']));
        if(!$data){         //判断数据是否为空，若为空跳转404页面
            header('Location:/blog_system/home/view/404.php');
            exit();
        }
        $type = new TypeModel();
        $type_data = $type->getTypeName(array('id'=>$data['blog_type']));        //查找 类别信息
        $data['blog_type'] = $type_data['name'];
        return $this->filter($data);
    }

    /**
     *  数据过滤
     */
    public function filter($data){
        $data['name'] = $data['name']=='admin'?'站长':$data['name'];
        $data['time'] =  date("Y-m-d H:i",$data['time']);       //发表时间
        return $data;
    }
    /**
     * 模糊查询博客
     */
    public function queryBlog($params){
        $sql = "SELECT id,title,outline,blog_img FROM blog_info WHERE title LIKE :title OR outline LIKE :outline AND state = 1 ";
        return $this->p_queryAll($sql,array('title'=>'%'.$params['key'].'%','outline'=>'%'.$params['key'].'%'));
    }
    /**
     * 查询点赞排行数据
     */
    public function queryPraise(){
        $sql = 'SELECT blog_info.id,blog_info.title FROM blog_info WHERE blog_info.state=1 ORDER BY blog_info.praise_count DESC LIMIT 0,8';
        return $this->queryAll($sql);
    }
    /**
     * 查询综合排名数据
     */
    public function queryRead(){
        $sql = 'SELECT blog_info.id,blog_info.title FROM blog_info WHERE blog_info.state=1 ORDER BY blog_info.reader DESC LIMIT 0,8';
        return $this->queryAll($sql);
    }
    /**
     * 查询文章分类及数量
     */
    public function queryType(){
        $sql = 'SELECT blog_type.id,blog_type.name,COUNT(blog_info.id) AS count FROM blog_info,blog_type
         WHERE blog_info.blog_type = blog_type.id AND blog_type.state=1 GROUP BY blog_type.id ORDER BY count DESC,name ASC ';
        return $this->queryAll($sql);
    }
    /**
     * 增加文章阅读数量
     */
    public function addRead($params){
        $sql = "UPDATE blog_info SET reader = reader+1 WHERE id = :id AND state = 1";
        $this->p_execute($sql,array('id'=>$params['id']));
    }
    /**
     * 增加文章点赞数量
     */
    public function addPrasise($params){
        $sql = "UPDATE blog_info SET praise_count = praise_count + 1 WHERE id = :id AND state = 1 ";
        $this->p_execute($sql,array('id'=>$params['id']));
    }
    /**
     * 查询指定类别博客信息
     */
    public function queryTypeBlog($params = array('id' => 0)){
        $sql = "SELECT id,title,outline,blog_img FROM blog_info WHERE blog_type = :id";
        return $this->p_queryAll($sql,array('id'=>$params['id']));
    }


}